/*******************************************************************************
 * Copyright (c) 2015 IBM Corp.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 *
 * The Eclipse Public License is available at
 *    http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 *   http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *    Jeffrey Dare - initial implementation and API implementation
 *******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <signal.h>
#include "iotfclient.h"

volatile int interrupt = 0;

// Handle signal interrupt
void sigHandler(int signo) {
	printf("SigINT received.\n");
	interrupt = 1;
}

void myCallback (char* commandName, char* format, void* payload)
{
	printf("------------------------------------\n" );
	printf("The command received :: %s\n", commandName);
	printf("format : %s\n", format);
	printf("Payload is : %s\n", (char *)payload);

	printf("------------------------------------\n" );
}

void GetTimeStr(char *timestr, time_t rawtime)
{	
	struct tm *info;	

	time(&rawtime);
	info = localtime(&rawtime);
	strftime(timestr, 80, "%Y-%m-%d %H:%M:%S", info);

	return;
}

void GetSendStr(char *sendstr, int int_temp_num, int float_press_num, int bool_valve_num)
{
	int len = 0;
	char timestr[32];
	int i;
	int ivalue;
	float fvalue;
	char bvalue[10];

	time_t t;
	srand((unsigned)time(&t));
	GetTimeStr(timestr, t);

	len += sprintf(sendstr+len, "%s", "{");

	i = 0;
	for (i = 0; i < int_temp_num; i++)
	{
		// 0 - 100
		ivalue = rand() % 100;
		len += sprintf(sendstr + len, "\"temp%d\":{\"v\":%d, \"t\":\"%s\"},", i, ivalue, timestr);		
	}

	i = 0;
	for (i = 0; i < float_press_num; i++)
	{
		// 0 - 99.99
		fvalue = ((float)(rand() % 10000)) / 100;
		len += sprintf(sendstr + len, "\"press%d\":{\"v\":%.2f, \"t\":\"%s\"},", i, fvalue, timestr);
	}

	i = 0;
	for (i = 0; i < bool_valve_num; i++)
	{
		// true or false
		ivalue = rand() % 2;
		if (ivalue == 0)
		{
			strcpy(bvalue, "false");
		}
		else
		{
			strcpy(bvalue, "true");
		}
		len += sprintf(sendstr + len, "\"valve%d\":{\"v\":%s, \"t\":\"%s\"},", i, bvalue, timestr);
	}

	len = len - 1;
	len += sprintf(sendstr + len, "%s", "}");

	return;
}

int main(int argc, char const *argv[])
{
	int rc = -1;

	Iotfclient client;

	char sendstr[1000]; 	// payload must less than 1000 each time publish.

	//catch interrupt signal
	signal(SIGINT, sigHandler);
	signal(SIGTERM, sigHandler);

	char *configFilePath = argv[1];
	//char *configFilePath = "./samples/device.cfg";

	rc = initialize_configfile(&client, configFilePath);

	if(rc != SUCCESS){
		printf("initialize failed and returned rc = %d.\n Quitting..", rc);
		return 0;
	}

	//unsigned short interval = 59;
	//setKeepAliveInterval(interval);

	rc = connectiotf(&client);

	if(rc != SUCCESS){
		printf("Connection failed and returned rc = %d.\n Quitting..", rc);
		return 0;
	}

	setCommandHandler(&client, myCallback);
	
	while(!interrupt) 
	{
		printf("Publishing the event stat with rc ");	
		memset(sendstr, '\0', 1000);
		GetSendStr(sendstr, 20, 0, 0);
		rc= publishEvent(&client, "rtvalue","json", sendstr, QOS1);
		printf(" %d\n", rc);
		yield(&client, 1000);

		printf("Publishing the event stat with rc ");
		memset(sendstr, '\0', 1000);
		GetSendStr(sendstr, 0, 20, 0);
		rc = publishEvent(&client, "rtvalue", "json", sendstr, QOS1);
		printf(" %d\n", rc);
		yield(&client, 1000);

		printf("Publishing the event stat with rc ");
		memset(sendstr, '\0', 1000);
		GetSendStr(sendstr, 0, 0, 10);
		rc = publishEvent(&client, "rtvalue", "json", sendstr, QOS1);
		printf(" %d\n", rc);
		yield(&client, 1000);

		sleep(3);
	}

	printf("Quitting!!\n");

	disconnect(&client);

	return 0;
}
